package com.takanafu.components{
	import mx.containers.TitleWindow;
	import flash.events.MouseEvent;
	import mx.events.FlexEvent;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.containers.ApplicationControlBar;
	import mx.core.EdgeMetrics;
	import mx.managers.CursorManager;
	import mx.core.Application;
	import flash.geom.Point;

	public class ResizableTitleWindow extends TitleWindow{
		
		[Embed(source="/assets/sysbtn_default.png")]
		private const _SYSBTN_DEFAULT:Class;
		[Embed(source="/assets/sysbtn_closeclick.png")]
		private const _SYSBTN_CLOSECLICK:Class;
		[Embed(source="/assets/sysbtn_closerollover.png")]
		private const _SYSBTN_CLOSEROLLOVER:Class;
		[Embed(source="/assets/sysbtn_maxclick.png")]
		private const _SYSBTN_MAXCLICK:Class;
		[Embed(source="/assets/sysbtn_maxrollover.png")]
		private const _SYSBTN_MAXROLLOVER:Class;
		[Embed(source="/assets/sysbtn_minclick.png")]
		private const _SYSBTN_MINCLICK:Class;
		[Embed(source="/assets/sysbtn_minrollover.png")]
		private const _SYSBTN_MINROLLOVER:Class;
		
		[Embed(source="/assets/diagonal1.png")]
		private const _RESIZE_DIAGONALRIGHTBOTTOM:Class;
		[Embed(source="/assets/vertical.png")]
		private const _RESIZE_VERTICAL:Class;
		[Embed(source="/assets/horizontal.png")]
		private const _RESIZE_HORIZONTAL:Class;
		
		private var oldHeight:Number;
		private var oldWidth:Number;
		private var oldX:Number;
		private var oldY:Number;
		
		private var nowCursor:Class = null;
		private const _MODE_UNRESIZE:int = 0;
		private const _MODE_VERTICAL:int = 1;
		private const _MODE_HORIZONTAL:int = 2;
		private const _MODE_DIAGONAL:int = 4;
		private var nowResizeMode:int = _MODE_UNRESIZE;
		private var point:Point = new Point();
		private var resizeTarget:ResizableTitleWindow;
		
		public function ResizableTitleWindow(){
			super();
			
			oldHeight = this.height;
			oldWidth = this.width;
			oldX = this.x;
			oldY = this.y;
			
			setStyle("borderColor",0xbfbfbf);
			setStyle("borderAlpha",1);
			setStyle("cornerRadius",2);
			setStyle("borderThicknessLeft",2);
			setStyle("borderThicknessRight",2);
			setStyle("borderThicknessBottom",15);
			addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove,false,0,true);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOut,false,0,true);
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown,false,0,true);
			addEventListener(FlexEvent.CREATION_COMPLETE,onCreationComplete,false,0,true);
			Application.application.parent.addEventListener(MouseEvent.MOUSE_UP,onMouseUp,false,0,true);
			Application.application.parent.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveResize,false,0,true);
		}
		
		private function onMouseMove(event:MouseEvent):void{
			if(resizeTarget == null){
				var xPos:Number = Application.application.parent.mouseX;
				var yPos:Number = Application.application.parent.mouseY;
				var xAbs:Number = (xPos - (this.x + this.width));
				var yAbs:Number = (yPos - (this.y + this.height));
	
				if((xAbs >= -8 && xAbs <= 4) && (yAbs >= -16 && yAbs <= 0)){
					nowResizeMode = _MODE_DIAGONAL;
					changeCursor(_RESIZE_DIAGONALRIGHTBOTTOM,-6,-6);
				}else if(xAbs >= -1 && xAbs <= 4){
					nowResizeMode = _MODE_HORIZONTAL;
					changeCursor(_RESIZE_HORIZONTAL,-9,-9);
				}else if(yAbs >= -8 && yAbs <= -4){
					nowResizeMode = _MODE_VERTICAL;
					changeCursor(_RESIZE_VERTICAL,-9,-9);
				}else{
					nowResizeMode = _MODE_UNRESIZE;
					changeCursor(null,0,0);
				}
			}
		}

		private function onMouseUp(event:MouseEvent):void{
			if(resizeTarget != null){
				resizeTarget.oldHeight = resizeTarget.height;
				resizeTarget.oldWidth = resizeTarget.width;
				resizeTarget.oldX = resizeTarget.x;
				resizeTarget.oldY = resizeTarget.y;
			}
			nowResizeMode = _MODE_UNRESIZE;
			resizeTarget = null;
		}
		private function onMouseMoveResize(event:MouseEvent):void{
			if(resizeTarget != null){
				resizeTarget.stopDragging();
				var xPlus:Number = Application.application.parent.mouseX - resizeTarget.point.x;
				var yPlus:Number = Application.application.parent.mouseY - resizeTarget.point.y;
				var minSize:Number = 50;

				if(nowResizeMode == _MODE_DIAGONAL || nowResizeMode == _MODE_HORIZONTAL){
					var wsize:Number = resizeTarget.oldWidth + xPlus > minSize ? resizeTarget.oldWidth + xPlus : minSize;

					resizeTarget.width = wsize;
				}
				if(nowResizeMode == _MODE_DIAGONAL || nowResizeMode == _MODE_VERTICAL){
					var hsize:Number = resizeTarget.oldHeight + yPlus > minSize ? resizeTarget.oldHeight + yPlus : minSize;

					resizeTarget.height = hsize;
				}
			}
		}
		private function onMouseOut(event:MouseEvent):void{
			if(resizeTarget == null){
				changeCursor(null,0,0);
			}
		}
		private function onMouseDown(event:MouseEvent):void{
			if(nowResizeMode != _MODE_UNRESIZE){
				resizeTarget = event.currentTarget as ResizableTitleWindow;
				
				if(resizeTarget != null){
					resizeTarget.stopDragging();
					resizeTarget.oldHeight = resizeTarget.height;
					resizeTarget.oldWidth = resizeTarget.width;
					resizeTarget.oldX = resizeTarget.x;
					resizeTarget.oldY = resizeTarget.y;
					point.x = resizeTarget.mouseX;
					point.y = resizeTarget.mouseY;
					point = this.localToGlobal(point);
				}
			}
		}
		private function onCreationComplete(event:FlexEvent):void{
			
		}
		
		
		private var _minButton:Button;
		private var _maxButton:Button;
		private var _closeButton:Button;

		[Embed(source="/assets/grip.png")]
		[Bindable]
		private var _resizeClass:Class;
		private var _resizeObj:Image;
		
		override protected function createChildren():void{
			super.createChildren();
			
			if(_minButton == null){
				_minButton = new Button();
				_minButton.width = 16;
				_minButton.height = 16;
				_minButton.toolTip = "最小化";
				_minButton.setStyle("upSkin",_SYSBTN_DEFAULT);
				_minButton.setStyle("overSkin",_SYSBTN_MINROLLOVER);
				_minButton.setStyle("downSkin",_SYSBTN_MINCLICK);
				titleBar.addChild(_minButton);
			}
			if(_maxButton == null){
				_maxButton = new Button();
				_maxButton.width = 16;
				_maxButton.height = 16;
				_maxButton.toolTip = "最大化";
				_maxButton.setStyle("upSkin",_SYSBTN_DEFAULT);
				_maxButton.setStyle("overSkin",_SYSBTN_MAXROLLOVER);
				_maxButton.setStyle("downSkin",_SYSBTN_MAXCLICK);
				titleBar.addChild(_maxButton);
			}
			if(_closeButton == null){
				_closeButton = new Button();
				_closeButton.width = 16;
				_closeButton.height = 16;
				_closeButton.toolTip = "閉じる";
				_closeButton.setStyle("upSkin",_SYSBTN_DEFAULT);
				_closeButton.setStyle("overSkin",_SYSBTN_CLOSEROLLOVER);
				_closeButton.setStyle("downSkin",_SYSBTN_CLOSECLICK);
				titleBar.addChild(_closeButton);
			}
			if(_resizeObj == null){
				_resizeObj = new Image();
				_resizeObj.source = _resizeClass;
				_resizeObj.width = 16;
				_resizeObj.height = 16;
				titleBar.addChild(_resizeObj);
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void{
			titleBar.width += 80;
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			var bm:EdgeMetrics = borderMetrics;
			var x:Number = bm.left;
	        var y:Number = bm.top;
	        
			if(_minButton != null){
				_minButton.move(unscaledWidth - x - bm.right - 60,5);
			}
			if(_maxButton != null){
				_maxButton.move(unscaledWidth - x - bm.right - 40,5);
			}
			if(_closeButton != null){
				_closeButton.move(unscaledWidth - x - bm.right - 20,5);
			}
			if(_resizeObj != null){
				_resizeObj.move(unscaledWidth - 16,unscaledHeight - 16);
			}
		}
		
		private function changeCursor(cur:Class,offx:Number,offy:Number):void{
			if(nowCursor != cur){
				nowCursor = cur;
				CursorManager.removeCursor(CursorManager.currentCursorID);
				
				if(cur != null){
					CursorManager.setCursor(cur,offx,offy);
				}
			}
		}
		
	}
}